| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194 |
1×
| import {
parse,
multiply,
rotateX,
rotateY,
rotateZ,
scale,
translateX,
translateY,
} from 'rematrix'
import { getPrefixedStyleProperty } from '../../utils/browser'
export default function style (element) {
var computed = window.getComputedStyle(element.node)
var position = computed.position
var config = element.config
/**
* Generate inline styles
*/
var inlineRegex = /.+[^;]/g
var inlineStyle = element.node.getAttribute('style') || ''
var inlineMatch = inlineRegex.exec(inlineStyle)
var inline = (inlineMatch) ? ((inlineMatch[0]) + ";") : ''
if (inline.indexOf('visibility: visible') === -1) {
inline += (inline.length) ? ' ' : ''
inline += 'visibility: visible;'
}
/**
* Generate opacity styles
*/
var computedOpacity = parseFloat(computed.opacity)
var configOpacity = !isNaN(parseFloat(config.opacity))
? parseFloat(config.opacity)
: parseFloat(computed.opacity)
var opacity = {
computed: (computedOpacity !== configOpacity) ? ("opacity: " + computedOpacity + ";") : '',
generated: (computedOpacity !== configOpacity) ? ("opacity: " + configOpacity + ";") : '',
}
/**
* Generate transformation styles
*/
var transformations = []
if (parseFloat(config.distance)) {
var axis = (config.origin === 'top' || config.origin === 'bottom') ? 'Y' : 'X'
/**
* Let’s make sure our our pixel distances are negative for top and left.
* e.g. { origin: 'top', distance: '25px' } starts at `top: -25px` in CSS.
*/
var distance = config.distance
if (config.origin === 'top' || config.origin === 'left') {
distance = /^-/.test(distance)
? distance.substr(1)
: ("-" + distance)
}
var ref = distance.match(/(^-?\d+\.?\d?)|(em$|px$|\%$)/g);
var value = ref[0];
var unit = ref[1];
switch (unit) {
case 'em':
distance = parseInt(computed.fontSize) * value
break
case 'px':
distance = value
break
case '%':
distance = (axis === 'Y')
? element.node.getBoundingClientRect().height * value / 100
: element.node.getBoundingClientRect().width * value / 100
break
default:
throw new RangeError('Unrecognized or missing distance unit.')
}
(axis === 'Y')
? transformations.push(translateY(distance))
: transformations.push(translateX(distance))
}
if (config.rotate.x) { transformations.push(rotateX(config.rotate.x)) }
if (config.rotate.y) { transformations.push(rotateY(config.rotate.y)) }
if (config.rotate.z) { transformations.push(rotateZ(config.rotate.z)) }
if (config.scale !== 1) {
config.scale === 0
? transformations.push(scale(0.0002))
: transformations.push(scale(config.scale))
}
var transform = {}
if (transformations.length) {
transform.property = getPrefixedStyleProperty('transform')
/**
* The default computed transform value should be one of:
* undefined || 'none' || 'matrix()' || 'matrix3d()'
*/
transform.computed = {
raw: computed[transform.property],
matrix: parse(computed[transform.property]),
}
transformations.unshift(transform.computed.matrix)
var product = transformations.reduce(multiply)
transform.generated = {
initial: ((transform.property) + ": matrix3d(" + (product.join(', ')) + ");"),
final: ((transform.property) + ": matrix3d(" + (transform.computed.matrix.join(', ')) + ");"),
}
} else {
transform.generated = {
initial: '',
final: '',
}
}
/**
* Generate transition styles
*/
var transition = {}
if (opacity.generated || transform.generated.initial) {
transition.property = getPrefixedStyleProperty('transition')
transition.computed = computed[transition.property]
transition.fragments = []
var delay = config.delay;
var duration = config.duration;
var easing = config.easing;
if (opacity.generated) {
transition.fragments.push({
delayed: ("opacity " + (duration / 1000) + "s " + easing + " " + (delay / 1000) + "s"),
instant: ("opacity " + (duration / 1000) + "s " + easing + " 0s"),
})
}
if (transform.generated.initial) {
transition.fragments.push({
delayed: ((transform.property) + " " + (duration / 1000) + "s " + easing + " " + (delay / 1000) + "s"),
instant: ((transform.property) + " " + (duration / 1000) + "s " + easing + " 0s"),
})
}
/**
* The default computed transition property should be one of:
* undefined || '' || 'all 0s ease 0s' || 'all 0s 0s cubic-bezier()'
*/
if (transition.computed && !transition.computed.match(/all 0s/)) {
transition.fragments.unshift({
delayed: transition.computed,
instant: transition.computed,
})
}
var composed = transition.fragments.reduce(function (composition, fragment, i) {
composition.delayed += (i === 0) ? fragment.delayed : (", " + (fragment.delayed))
composition.instant += (i === 0) ? fragment.instant : (", " + (fragment.instant))
return composition
}, {
delayed: '',
instant: '',
})
transition.generated = {
delayed: ((transition.property) + ": " + (composed.delayed) + ";"),
instant: ((transition.property) + ": " + (composed.instant) + ";"),
}
} else {
transition.generated = {
delayed: '',
instant: '',
}
}
return {
inline: inline,
opacity: opacity,
position: position,
transform: transform,
transition: transition,
}
}
|